package com.dant.components.containers.panels
{
	import com.dant.events.AdvancedPanelEvent;
	import com.dant.interfaces.IDragDropManager;
	import com.dant.interfaces.IPanelsFocusManager;
	import com.dant.interfaces.IResizeManager;
	import com.dant.interfaces.IViewPanel;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.effects.Effect;
	import mx.events.EffectEvent;
	
	//events
	[Event(name="minWindow", type="com.dant.events.AdvancedPanelEvent")]
	[Event(name="maxWindow", type="com.dant.events.AdvancedPanelEvent")]
	[Event(name="restoreWindow", type="com.dant.events.AdvancedPanelEvent")]
	[Event(name="closeWindow", type="com.dant.events.AdvancedPanelEvent")]
	[Event(name="minimizedWindow", type="com.dant.events.AdvancedPanelEvent")]
	[Event(name="maximizedWindow", type="com.dant.events.AdvancedPanelEvent")]
	[Event(name="restoredWindow", type="com.dant.events.AdvancedPanelEvent")]
	[Event(name="closedWindow", type="com.dant.events.AdvancedPanelEvent")]

	[Effect(name="maximizeEffect", event="maxWindow")]
	[Effect(name="restoreEffect", event="restoreWindow")]
	[Effect(name="minimizeEffect", event="minWindow")]
	[Effect(name="closeEffect", event="closeWindow")]

    [Style(name="closeWindowButtonSkin",type="Class",inherit="no")]
    [Style(name="closeWindowButtonDownSkin",type="Class",inherit="no")]
    [Style(name="closeWindowButtonOverSkin",type="Class",inherit="no")]
    [Style(name="minimizeWindowButtonSkin",type="Class",inherit="no")]
    [Style(name="minimizeWindowButtonDownSkin",type="Class",inherit="no")]
    [Style(name="minimizeWindowButtonOverSkin",type="Class",inherit="no")]
    [Style(name="maximizeWindowButtonSkin",type="Class",inherit="no")]
    [Style(name="maximizeWindowButtonDownSkin",type="Class",inherit="no")]
    [Style(name="maximizeWindowButtonOverSkin",type="Class",inherit="no")]
    [Style(name="restoreWindowButtonSkin",type="Class",inherit="no")]
    [Style(name="restoreWindowButtonDownSkin",type="Class",inherit="no")]
    [Style(name="restoreWindowButtonOverSkin",type="Class",inherit="no")]
	public class AdvancedPanel extends Panel implements IViewPanel
	{
		private static const CONST_SPACE_BETWEEN_TITLE_BUTTONS:Number = 6;
		private static const CONST_TITLE_BUTTONS_HEIGHT:Number = 10;
		private static const CONST_TITLE_BUTTONS_WIDTH:Number = 10;
		private static const CONST_MINIMIZED_WIDTH:Number = 100;
		private static const CONST_NUMBER_OF_BUTTONS:Number = 3;

		override public function set height(value:Number):void
		{
            if ( isMaximized && !enableOverrideSize )
            {
                _isMaximized = false;
                endRestoreActions();
            }

			if ( isMinimized && !enableOverrideSize )
			{
				return;
			}

	        super.height = value;
	        invalidateDisplayList();
		}

		override public function set width(value:Number):void
		{
		    if ( isMaximized && !enableOverrideSize )
		    {
		        _isMaximized = false;
		        endRestoreActions();
		    }

            if ( isMinimized && !enableOverrideSize )
            {
            	return;
            }
            super.width = value;
			trace ("ADVANCEDPANEL - width: "+value);
            invalidateDisplayList();
		}

		override public function set x(value:Number):void
		{
			if ( isMaximized )
            {
            	return;
            }
            super.x = value;
		}

        override public function set y(value:Number):void
        {
        	if ( isMaximized )
            {
            	return;
            }
            super.y = value;
        }

		// show buttons
		protected var _showMinButton:Boolean = false;		
		protected var _showMaxButton:Boolean = false;		
		protected var _showCloseButton:Boolean = false;
		
		protected var _showRestoreButtonWhenMinimized:Boolean;
		
        private var showMinButtonChanged:Boolean = true;
        private var showCloseButtonChanged:Boolean = true;
        private var showMaxButtonChanged:Boolean = true;
        private var showRestoreButtonWhenMinimizedChanged:Boolean = true;

		// title bar buttons
		protected var windowMinButton:Button;
		protected var windowRestoreButton:Button;
		protected var windowCloseButton:Button;

		// positioning properties
		protected var _restoreHeight:int;
		protected var _restoreWidth:int;		
		protected var _oldX:Number = 0;
		protected var _oldY:Number = 0;
		
		// determines if the panel is Maximized or Minimized
		protected var _isMinimized:Boolean = false; 
		protected var _isMaximized:Boolean = false;
		protected var _isRestored:Boolean = true;
		protected var isMinimizing:Boolean = false;
		protected var isMaximizing:Boolean = false;
		protected var isRestoring:Boolean = false;

		// position and size of the resize handler
		private var oW:Number;
		private var oH:Number;
		private var oX:Number;
		private var oY:Number;				
		
		// determines if the panel is focused
		private var _isFocused:Boolean = false;
		
		private var isFocusedChanged : Boolean;

		private var isFocusedEnabled : Boolean;

		// color for when the panel is focused
		protected var _focusedHeaderColorFrom:uint = 0xC3D1D9; // TODO: Change this to Style property
		protected var _focusedHeaderColorTo:uint = 0x5A788A; // TODO: Change this to Style property
		protected var _focusedBorderColor:uint = 0x5A788A; // TODO: Change this to Style property

		// color for when the panel is not focused
		protected var _notFocusedHeaderColorFrom:uint = 0xC3D1D9; // TODO: Change this to Style property
		protected var _notFocusedHeaderColorTo:uint = 0xD2DCE2; // TODO: Change this to Style property
		protected var _notFocusedBorderColor:uint = 0xD2DCE2; // TODO: Change this to Style property

        protected var enableOverrideSize:Boolean;
        protected var widthChanged:Boolean;

		// used to set the panel index when is dropped into the parent.
		private var currentIndex:Number = 0;

		private var _dragDropManager:IDragDropManager;

		private var _resizeManager:IResizeManager;

		private var _panelFocusManager:IPanelsFocusManager;


		// auto controls the dragging and dropping of the panel from the titleBar.
		// if it's turned on, a drag manager instance must be provided
		private var _enableSelfControlledDragAndDrop:Boolean;
		private var enableSelfControlledDragAndDropChanged:Boolean;

		// auto controls the resize of the panel from the corners.
		// if it's turned on, a resize manager instance must be provided
		private var _enableSelfControlledResize:Boolean;
		private var enableSelfControlledResizeChanged:Boolean;

		// auto controls the resize of the panel from the corners.
		// if it's turned on, a resize manager instance must be provided
		private var _enableSelfControlledFocus:Boolean;
		private var enableSelfControlledFocusChanged:Boolean;

		// used to know is the panel is to be opened in full screen mode
		private var _openInFullScreenMode:Boolean;

        [Inspectable(type="Number")]
        public function set restoreHeight (value:Number):void
        {
            _restoreHeight = value;
        }
        
        [Inspectable(type="Number")]
        public function set restoreWidth (value:Number):void
        {
            _restoreWidth = value;
        }
        
        [Inspectable(type="Number")]
        public function set restoreX (value:Number):void
        {
            _oldX = value;
        }
        
        [Inspectable(type="Number")]
        public function set restoreY (value:Number):void
        {
            _oldY = value;
        }
		
        // Expose the title bar property for dragging and dropping.
        [Bindable]
        public function set customTitleBar( value:UIComponent):void
        {
            titleBar = value;
        }

        public function get customTitleBar ( ) : UIComponent
        {
            return ( titleBar );
        }

		[Bindable]
        public function set dragDropManager ( value:IDragDropManager ):void
        {
        	if ( _dragDropManager && _dragDropManager.objectIsDraggable(this) )
        	{
        		unregisterFromDragAndDrop();
        	}
        	_dragDropManager = value;
            enableSelfControlledDragAndDropChanged = true;
            invalidateProperties();
        }
        
        public function get dragDropManager () : IDragDropManager
        {
            return ( _dragDropManager );
        }

		[Bindable]
        public function set resizeManager ( value:IResizeManager ):void
        {
        	if ( _resizeManager && _resizeManager.objectIsResizable(this) )
        	{
        		unregisterFromResize();
        	}
        	_resizeManager = value;
            enableSelfControlledResizeChanged = true;
            invalidateProperties();
            invalidateDisplayList();
        }
        
        public function get resizeManager () : IResizeManager
        {
            return ( _resizeManager );
        }

		[Bindable]
        public function set panelFocusManager ( value:IPanelsFocusManager ):void
        {
        	if ( _panelFocusManager && _panelFocusManager.objectIsFocusable(this) )
        	{
        		unregisterFromFocusManager();
        	}
        	_panelFocusManager = value;
            enableSelfControlledFocusChanged = true;
            invalidateProperties();
        }
        
        public function get panelFocusManager () : IPanelsFocusManager
        {
            return ( _panelFocusManager );
        }

		[Bindable]
        [Inspectable(type="Boolean")]
        public function set enableSelfControlledDragAndDrop ( value:Boolean ):void
        {
            _enableSelfControlledDragAndDrop = value;
            enableSelfControlledDragAndDropChanged = true;
            invalidateProperties();
        }

        public function get enableSelfControlledDragAndDrop () : Boolean
        {
            return ( _enableSelfControlledDragAndDrop );
        }

		[Bindable]
        [Inspectable(type="Boolean")]
        public function set enableSelfControlledResize ( value:Boolean ):void
        {
            _enableSelfControlledResize = value;
            enableSelfControlledResizeChanged = true;
            invalidateProperties();
            invalidateDisplayList();
        }

        public function get enableSelfControlledResize () : Boolean
        {
            return ( _enableSelfControlledResize );
        }

		[Bindable]
        [Inspectable(type="Boolean")]
        public function set enableSelfControlledFocus ( value:Boolean ):void
        {
            _enableSelfControlledFocus = value;
            enableSelfControlledFocusChanged = true;
            invalidateProperties();
        }

        public function get enableSelfControlledFocus () : Boolean
        {
            return ( _enableSelfControlledFocus );
        }

		// Color of the header when focused
		[Bindable]
		[Inspectable(type="Color")]
		public function set focusedHeaderColorFrom ( color:uint ):void
		{
			_focusedHeaderColorFrom = color;
		}

        public function get focusedHeaderColorFrom ( ):uint
        {
            return (_focusedHeaderColorFrom);
        }
		
		// Color of the header when focused
		[Bindable]
		[Inspectable(type="Color")]
		public function set focusedHeaderColorTo ( color:uint ):void
		{
			_focusedHeaderColorTo = color;
		}
		
        public function get focusedHeaderColorTo ( ):uint
        {
            return (_focusedHeaderColorTo);
        }
		
		// Color of the border when focused
		[Bindable]
		[Inspectable(type="Color")]
		public function set focusedBorderColor ( value:uint ):void
		{
			_focusedBorderColor = value;
		}
		
        public function get focusedBorderColor ( ):uint
        {
            return ( _focusedBorderColor );
        }

		// Color of the border when not focused
		[Bindable]
		[Inspectable(type="Color")]
		public function set notFocusedHeaderColorFrom ( value:uint ):void
		{
			_notFocusedHeaderColorFrom = value;
		}
		
        public function get notFocusedHeaderColorFrom ( ):uint
        {
            return ( _notFocusedHeaderColorFrom );
        }

		// Color of the header when not focused
		[Bindable]
		[Inspectable(type="Color")]
		public function set notFocusedHeaderColorTo ( value:uint ):void
		{
			_notFocusedHeaderColorTo = value;
		}
		
        public function get notFocusedHeaderColorTo ( ):uint
        {
            return (_notFocusedHeaderColorTo);
        }

		// Color of the header when not focused
		[Bindable]
		[Inspectable(type="Color")]
		public function set notFocusedBorderColor ( value:uint ):void
		{
			_notFocusedBorderColor = value;
		}
		
		public function get notFocusedBorderColor (): uint
		{
		    return ( _notFocusedBorderColor );
		}
		
		public function set openInFullScreenMode ( value:Boolean ) :void
		{
		    _openInFullScreenMode = value;
		}
		
		public function get openInFullScreenMode ( ) :Boolean
		{
		    return ( _openInFullScreenMode );
		}

        [Bindable]
		[Inspectable(type="Boolean")]
		public function set showMinButton (value:Boolean):void
		{
			_showMinButton = value;
			showMinButtonChanged = true;
			invalidateProperties();
		}
		
		[Inspectable(type="Boolean")]
		public function get showMinButton ():Boolean
		{
			return (_showMinButton);
		}
		
		[Bindable]
		[Inspectable(type="Boolean")]
		public function set showCloseButton (value:Boolean):void
		{
			_showCloseButton = value;
			showCloseButtonChanged = true;
			invalidateProperties();
		}
		
		[Inspectable(type="Boolean")]
		public function get showCloseButton ():Boolean
		{
			return (_showCloseButton);
		}		
		
		[Bindable]
		[Inspectable(type="Boolean")]
		public function set showMaxButton (value:Boolean):void
		{
			_showMaxButton = value;
			showMaxButtonChanged = true;
			invalidateProperties();
		}

        [Inspectable(type="Boolean")]
        public function get showMaxButton ():Boolean
        {
            return (_showMaxButton);
        }

        [Bindable]
        [Inspectable(type="Boolean")]
        public function set showRestoreButtonWhenMinimized (value:Boolean):void
        {
            _showRestoreButtonWhenMinimized = value;
            showRestoreButtonWhenMinimized = true;
            invalidateProperties();
        }

        [Inspectable(type="Boolean")]
        public function get showRestoreButtonWhenMinimized ():Boolean
        {
            return (_showRestoreButtonWhenMinimized);
        }

		public function get isMinimized ():Boolean
		{
			return (_isMinimized);
		}
		
		public function get isMaximized ():Boolean
		{
			return (_isMaximized);
		}	
		

		public function AdvancedPanel ( )
		{
			super();
		}
		
//--------------------------------------------------------------------------
//
//  Overriden methods
//
//--------------------------------------------------------------------------
		
		/**
		 * Overrides the createChildren function. The buttons are created here.
		 */
		override protected function createChildren() : void
		{
			super.createChildren();
		}


        override protected function commitProperties():void
        {
            super.commitProperties();
            
            var layoutButtonsRequired:Boolean;
            
            if ( enableSelfControlledDragAndDropChanged )
            {
            	if (dragDropManager)
            	{
	            	if (enableSelfControlledDragAndDrop)
	            	{
	            		registerToDragAndDrop();
	            	}
	            	else
	            	{
	            		unregisterFromDragAndDrop();
	            	}
            	}
            	enableSelfControlledDragAndDropChanged = false;
            }

            if ( enableSelfControlledResizeChanged )
            {
            	if (resizeManager)
            	{
	            	if (enableSelfControlledResize)
	            	{
	            		registerToResize();
	            	}
	            	else
	            	{
	            		unregisterFromResize();
	            	}
            	}
            	enableSelfControlledResizeChanged = false;
            }

            if ( enableSelfControlledFocusChanged )
            {
            	if (panelFocusManager)
            	{
	            	if (enableSelfControlledFocus)
	            	{
	            		registerToFocusManager();
	            	}
	            	else
	            	{
	            		unregisterFromFocusManager();
	            	}
            	}
            	enableSelfControlledFocusChanged = false;
            }


            if ( showCloseButtonChanged )
            {
                // if the close button is visible
                if( _showCloseButton)
                {
                    addCloseButton();
                }
                else if ( windowCloseButton )
                {
                	removeCloseButton();
                }
                layoutButtonsRequired = true;
                showCloseButtonChanged = false;
            }

            if ( showMaxButtonChanged )
            {
                titleBar.addEventListener(MouseEvent.DOUBLE_CLICK,
                           doubleClickHandler,false,0,true);
                // if the restore button is visible
                if(_showMaxButton)
                {
                	addMaximizeButton();
                }
                else if ( windowRestoreButton )
                {
                	removeMaximizeButton();
                }
                layoutButtonsRequired = true;
                showMaxButtonChanged = false;
            }

            if ( showMinButtonChanged )
            {
                // if the minimize button is visible
                if ( _showMinButton )
                {
                	addMinimizeButton();
                }
                else if ( windowMinButton )
                {
                	removeMinimizeButton();
                }
                layoutButtonsRequired = true;
                showMinButtonChanged = false;
            }

            if ( layoutButtonsRequired )
            {
                layoutWindowButtons(); // layouts the buttons
                layoutButtonsRequired = false;
            }
        }


        /**
         * Overrides the updateDisplayList method
         */
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);

            var arr_buttons : Array;
            var arr_show_em : Array;
            var i : int;

            graphics.clear();

            // if its not maximized, sets the maximize skin to the restore button.
            if ( showMaxButton || (isMinimized&&showRestoreButtonWhenMinimized) )
            {
                if ( _isMaximized || (isMinimized&&showRestoreButtonWhenMinimized) )
                {
                    skinRestoreButton();                    
                }
                else
                {
                    skinMaximizeButton();
                }
            }
 
            if ( ! isMinimized && ! isMaximized && enableSelfControlledResize && resizeManager )
            {
                graphics.lineStyle(2);
                graphics.moveTo(unscaledWidth - 6, unscaledHeight - 1)
                graphics.curveTo(unscaledWidth - 3, unscaledHeight - 3, unscaledWidth - 1, unscaledHeight - 6);                     
                graphics.moveTo(unscaledWidth - 6, unscaledHeight - 4)
                graphics.curveTo(unscaledWidth - 5, unscaledHeight - 5, unscaledWidth - 4, unscaledHeight - 6);
            }                     

            layoutWindowButtons();
        }

        /**
         * Puts the focus on the selected panel
         */
        override public function setFocus():void 
        {
            //super.setFocus();
        }

//--------------------------------------------------------------------------
//
//  Methods
//
//--------------------------------------------------------------------------
		
		protected function shouldRegisterForDragAndDrop():Boolean
		{
		    if ( !dragDropManager )
		    {
		        return (false);
		    }
			var alreadyRegistered:Boolean = dragDropManager.objectIsDraggable(this);
			return (enableSelfControlledDragAndDrop && dragDropManager && 
								!alreadyRegistered);
		}

		protected function shouldRegisterForResize():Boolean
		{
		    if ( ! resizeManager )
		    {
		        return (false);
		    }
			var alreadyRegistered:Boolean = resizeManager.objectIsResizable(this);
			return (enableSelfControlledResize && resizeManager && 
								!alreadyRegistered);
		}

		protected function shouldRegisterForFocusManager():Boolean
		{
		    if ( ! panelFocusManager )
		    {
		        return (false);
		    }
			var alreadyRegistered:Boolean = panelFocusManager.objectIsFocusable(this);
			return (enableSelfControlledFocus && panelFocusManager && 
								!alreadyRegistered);
		}
		
		protected function registerToDragAndDrop():Boolean
		{
			if (shouldRegisterForDragAndDrop())
			{
				dragDropManager.registerObjectToDragAndDrop(this);
				return(true);
			}
			return (false);
		}

		protected function unregisterFromDragAndDrop():Boolean
		{
			if (dragDropManager)
			{
				dragDropManager.unregisterObjectToDragAndDrop(this);
				return(true);
			}
			return (false);
		}

		protected function registerToResize():Boolean
		{
			if (shouldRegisterForResize())
			{
				resizeManager.registerObjectToResize(this,false,false,false,
	             							 false,true,false,false,false);
				return(true);
			}
			return (false);
		}

		protected function unregisterFromResize():Boolean
		{
			if (resizeManager)
			{
				resizeManager.unregisterObjectToResize(this);
				return(true);
			}
			return (false);
		}

		protected function registerToFocusManager():Boolean
		{
			if (shouldRegisterForFocusManager())
			{
				panelFocusManager.registerObjectToFocusManager(this);
				return(true);
			}
			return (false);
		}

		protected function unregisterFromFocusManager():Boolean
		{
			if (panelFocusManager)
			{
				panelFocusManager.unregisterObjectToFocus(this);
				return(true);
			}
			return (false);
		}
		
		/**
		 * Sets the position of the buttons depending on the number of buttons and the size if the titlebar
		 */
		protected function layoutWindowButtons():void
		{
		    var totalWidthOfButtons:Number = CONST_SPACE_BETWEEN_TITLE_BUTTONS;
 			// array of the buttons
			var buttonsArray:Array = [windowCloseButton, windowRestoreButton, windowMinButton];
			// associated with the array of visibility for each button
			var showEmArray:Array = [_showCloseButton, 
			                        (_showMaxButton || 
			                        (_showRestoreButtonWhenMinimized&&_isMinimized)),
			                        _showMinButton ];
			// position of the visible button
			var pos1:Number = 1;
			var pos2:Number = -1 * CONST_SPACE_BETWEEN_TITLE_BUTTONS;
			var n:int = buttonsArray.length;
			for (var i:Number = 0; i < n; i++)
			{
				// if the button is visible and is not null
				if ((buttonsArray[i] != null) && (showEmArray[i] != false) 
				                && (titleBar.contains(buttonsArray[i])==true) )
				{
					var button:UIComponent = buttonsArray[i];
					var buttonWidth:Number = 
					       (button.width>0)?button.width:CONST_TITLE_BUTTONS_WIDTH;
					var buttonHeight:Number = 
					       (button.height>0)?button.height:CONST_TITLE_BUTTONS_HEIGHT;
					// it is moved to it's place
					button.move (titleBar.width - buttonWidth * pos1 + pos2, 
					                     (titleBar.height - buttonHeight) / 2);
					pos1++;
					pos2 = pos2 - CONST_SPACE_BETWEEN_TITLE_BUTTONS;
					
					totalWidthOfButtons = totalWidthOfButtons + buttonWidth + CONST_SPACE_BETWEEN_TITLE_BUTTONS;
				}
			}
			
			titleTextField.width = titleBar.width - totalWidthOfButtons - CONST_SPACE_BETWEEN_TITLE_BUTTONS;
			titleTextField.multiline = false;
			if (titleTextField.measuredWidth > titleTextField.width)
			{
    			trace(titleTextField.truncateToFit("..."));
			}
			else
			{
			    titleTextField.text = title;
//			    trace(titleTextField.truncateToFit(""));
			}
		}

		//--------------------------------------
		//  Add buttons to titleBar
		//--------------------------------------
		protected function initializeTitleBarButton(button:Button,skinFunction:Function):void
		{
            button.width=10;
            button.height=10;
            button.focusEnabled=false;
            skinFunction();
		}

		protected function addButtonToTitleBar(button:Button,
											   mouseDownHandler:Function):DisplayObject
		{
            if (! titleBar.contains(button))
            {
                button.addEventListener(MouseEvent.MOUSE_DOWN,
                          mouseDownHandler,false,0,true);
                return (titleBar.addChild(button));
            }
            return (button);
		}

		protected function addCloseButton():DisplayObject
		{
            if ( ! windowCloseButton && _showCloseButton )
            {
                windowCloseButton = new Button();
                initializeTitleBarButton(windowCloseButton,skinCloseButton);
            }
			return (addButtonToTitleBar(windowCloseButton,windowCloseButton_mouseDownHandler));
        }

        protected function addMinimizeButton():DisplayObject
        {
            if( ! windowMinButton && _showMinButton )
            {
                windowMinButton = new Button();
                initializeTitleBarButton(windowMinButton,skinMinimizeButton);
            }
            return (addButtonToTitleBar(windowMinButton,windowMinButton_mouseDownHandler));
        }

        protected function addMaximizeButton():DisplayObject
        {
            if ( ! windowRestoreButton )
            {
	            windowRestoreButton = new Button();
	            initializeTitleBarButton(windowRestoreButton, 
	            						( isMaximized || isMinimized )?
	            						skinRestoreButton:skinMaximizeButton);
	        }
            else if ( isMaximized || isMinimized )
            {
                skinRestoreButton();
            }
            else
            {
                skinMaximizeButton();
            }
			return (addButtonToTitleBar(windowRestoreButton,windowRestoreButton_mouseDownHandler));
        }


		//--------------------------------------
		//  Remove buttons from titleBar
		//--------------------------------------

		protected function removeButtonFromTitleBar(button:Button,
							mouseDownHandler:Function):DisplayObject
		{
			if (!button)
			{
				return (null);
			}
            button.removeEventListener(MouseEvent.MOUSE_DOWN,
                          							mouseDownHandler);
            if ( titleBar.contains(button) )
            {
                return(titleBar.removeChild(button));
            }
            return (button);
		}

        protected function removeCloseButton():DisplayObject
        {
        	return (removeButtonFromTitleBar(windowCloseButton, 
        							windowCloseButton_mouseDownHandler));
        }

        protected function removeMinimizeButton():DisplayObject
        {
			return (removeButtonFromTitleBar(windowMinButton, 
										windowMinButton_mouseDownHandler));
        }

        protected function removeMaximizeButton():DisplayObject
        {
            titleBar.removeEventListener(MouseEvent.DOUBLE_CLICK,
                                       doubleClickHandler);
			return (removeButtonFromTitleBar(windowRestoreButton, 
										windowRestoreButton_mouseDownHandler));
        }

		//--------------------------------------
		//  Panel actions
		//--------------------------------------

        public function removeFocus():void
        {
            // override and implement
        }
        
		protected function calculateMinimizedHeight():int
		{
			var minimizedHeight:int = 0;
            if (titleBar)
            {
                minimizedHeight = minimizedHeight+titleBar.height;
            }
            if (controlBar)
            {
                minimizedHeight = minimizedHeight+controlBar.height;
            }
            return (minimizedHeight);
		}
		
		protected function saveCurrentDimension(x:int,y:int,width:Number,height:Number):void
		{
            _oldX = x;
            _oldY = y;
            _restoreHeight = height;    
            _restoreWidth = width;
		}

		public function minimize () : void
		{
			var targetWidth:Number = width;
			var targetHeight:Number = minimizedHeight;
			var minimizeEvent:AdvancedPanelEvent = dispatchMinEvent(width,height,
																	targetWidth,
																	targetHeight,
																	x,y);

			if ( minimizeEvent.isDefaultPrevented() )
			{
				return;
			}

            // if its going to be minimized / if its not already minimized
            if (_isMinimized)
            {
            	return;
            }

            if ( ! _isMaximized )
            {
            	saveCurrentDimension(x,y,width,height);
            }

            // calculates the height of the minimized panel
            var minimizedHeight:int = calculateMinimizedHeight();;
            _isRestored = false;

			// can't resize if minimized
			if ( enableSelfControlledResize )
			{
				unregisterFromResize();
			}

            startMinimize();
            removeMinimizeButton();
            addMaximizeButton();
            assignMinimizeSizeToPanel (targetWidth,targetHeight);

            addEventListener(MouseEvent.CLICK,panelClickHandler,false,0,true);
		}

		public function maximize () : void
		{
			var targetHeight:Number = parent.height - getHeightBorderThicknessDefault();
			var targetWidth:Number = parent.width - getWidthBorderThicknessDefault();
			var targetX:Number = 0;
			var targetY:Number = 0;
			
			var maximizeEvent:AdvancedPanelEvent = 
								dispatchMaxEvent(width,height,
												 targetWidth,targetHeight,
												 x,y,targetX,targetY);
			if ( maximizeEvent.isDefaultPrevented() )
			{
				return;
			}
			
            removeEventListener(MouseEvent.CLICK,panelClickHandler);

            if ( _isMinimized )
            {
            	_isMinimized = false;
                if ( ! showMaxButton && showRestoreButtonWhenMinimized )
                {
                    removeMaximizeButton();
                }
                if ( showMinButton )
                {
                    addMinimizeButton();
                }
            	addMinimizeButton();
            	layoutWindowButtons();
            }
            
            if ( _isRestored )
            {
	            // backup of it's size and position
	            saveCurrentDimension(x,y,width,height);
            	_isRestored = false;
            }

			// shouldnt drag & drop if it's maximized
			if ( enableSelfControlledDragAndDrop )
			{
				unregisterFromDragAndDrop();
			}

            // sets new size and position
            assignMaximizeSizeToPanel(targetHeight,targetWidth,targetX,targetY);

            // brings the panel to the front of it's parent.
            parent.setChildIndex(Panel(this), parent.numChildren-1);            

            invalidateProperties();
		}

		public function restore () : void
		{
			var restoreEvent:AdvancedPanelEvent = 
							dispatchRestoreEvent(width,height,
												_restoreWidth,
												_restoreHeight,
												x,y);
			if ( restoreEvent.isDefaultPrevented() )
			{
				return;
			}
			
            removeEventListener(MouseEvent.CLICK,panelClickHandler);

            // if the panel is minimized
            if (_isMinimized)
            {
                //set isMinimized to false
                _isMinimized = false;
                
                if ( shouldRegisterForResize() )
                {
                	registerToResize();
                }

                if ( ! showMaxButton && showRestoreButtonWhenMinimized )
                {
                	removeMaximizeButton();
                }
                if ( showMinButton )
                {
                    addMinimizeButton();
                }
                layoutWindowButtons();
            }

            if (_isMaximized)
            {
	            //sets isMaximized to false
	            _isMaximized = false;
	            
	            if (shouldRegisterForDragAndDrop())
	            {
	            	registerToDragAndDrop();
	            }
            }

            // restores the size and position of the component
            assignRestoreSizeToPanel(_restoreHeight, _restoreWidth, _oldX, _oldY);
		}

		public function close () : void
		{
			var closeEvent:AdvancedPanelEvent = dispatchCloseEvent(width,height,
																  NaN,NaN,
																  x,y);
			
			if ( closeEvent.isDefaultPrevented() )
			{
				return;
			}
            removeEventListener(MouseEvent.CLICK, panelClickHandler);
            
            removeHeaderButtons();
            title = "";
            
            assignCloseSizeToPanel();
		}

		//--------------------------------------
		//  Actions size changes
		//--------------------------------------

        protected function assignMinimizeSizeToPanel ( minimizedWidth:Number, minimizedHeight:Number ) : void
        {
        	startMinimize();

        	var oldHeight:Number = height;
        	var oldWidth:Number = width;

            enableOverrideSize = true;

            width = minimizedWidth;
            height = minimizedHeight;

            var effect:Effect = Effect(getStyle("minimizeEffect"));

            if ( effect )
            {
                effect.addEventListener(EffectEvent.EFFECT_END,
                           minimizeEffect_effectEndHandler,false,0,true);
            }
            else
            {
            	endMinimizeActions();
            }
        }

        /**
         * Assigns the new size and position to maximize the panel.
         */
        protected function assignMaximizeSizeToPanel ( newHeight : Number, newWidth : Number, newX : Number, newY : Number  ) : void
        {
        	startMaximize();

            var oldHeight:Number = height;
            var oldWidth:Number = width;
            var oldX:Number = x;
            var oldY:Number = y;

            enableOverrideSize = true;

            var effect:Effect = Effect(getStyle("maximizeEffect"));

            if ( effect )
            {
                effect.addEventListener(EffectEvent.EFFECT_END,
                           maximizeEffect_effectEndHandler,false,0,true);
            }
            else
            {
				width = newWidth;
				height = newHeight;
				x = newX;
				y = newY;
            	endMaximizeActions();
            }
        }

        /**
         * Assigns the new size and position to restore the panel.
         */
        protected function assignRestoreSizeToPanel ( newHeight : Number, newWidth : Number, newX : Number, newY : Number  ) : void
        {
            startRestore();

            var oldHeight:Number = height;
            var oldWidth:Number = width;
            var oldX:Number = x;
            var oldY:Number = y;

            enableOverrideSize = true;
            
            var effect:Effect = Effect(getStyle("restoreEffect"));

            if ( effect )
            {
                effect.addEventListener(EffectEvent.EFFECT_END,
                           restoreEffect_effectEndHandler,false,0,true);
            }
            else
            {
				width = newWidth;
				height = newHeight;
				x = newX;
				y = newY;
            	endRestoreActions();
            }
        }

        /**
         * Assigns the new size and position to restore the panel.
         */
        protected function assignCloseSizeToPanel ( ) : void
        {
//            startRestore();
			startClose();

            enableOverrideSize = true;
            
            var effect:Effect = Effect(getStyle("closeEffect"));

            if ( effect )
            {
                effect.addEventListener(EffectEvent.EFFECT_END,
                           closeEffect_effectEndHandler,false,0,true);
            }
            else
            {
            	endCloseActions();
            }
        }

		//--------------------------------------
		//  End actions
		//--------------------------------------

        protected function endMinimizeActions():void
        {
            endMinimize();
            _isMinimized = true;
            if ( ! showMaxButton && showRestoreButtonWhenMinimized )
            {
                addMaximizeButton();
                layoutWindowButtons();
            }
            invalidateDisplayList();

            enableOverrideSize = false;

            dispatchMinimizedEvent( );
        }

        protected function endMaximizeActions():void
        {
            endMaximize();
            _isMaximized = true;
            invalidateDisplayList();
            enableOverrideSize = false;
            dispatchMaximizedEvent();
        }

        protected function endRestoreActions():void
        {
            endRestore();
            _isRestored = true;
            if ( ! showMaxButton && showRestoreButtonWhenMinimized )
            {
                removeMaximizeButton();
                layoutWindowButtons();
            }
            invalidateDisplayList();
            enableOverrideSize = false;
            dispatchRestoredEvent();
        }

        protected function endCloseActions():void
        {
        	if ( enableSelfControlledDragAndDrop )
        	{
        		unregisterFromDragAndDrop();
        	}

        	if ( enableSelfControlledResize )
        	{
        		unregisterFromResize();
        	}
        	
        	if ( enableSelfControlledFocus )
        	{
        		unregisterFromFocusManager();
        	}
        	
            dispatchClosedEvent();
            removeFromParent();
        }

        protected function removeFromParent ( ):void
        {
            var panel : AdvancedPanel = AdvancedPanel( parent.removeChild( this ) );
            dispatchClosedEvent ( panel );
        }

        protected function removeHeaderButtons ( ) : void
        {
            if(windowMinButton != null && _showMinButton && !isMinimized)
            {
                windowMinButton.removeEventListener(MouseEvent.MOUSE_DOWN, windowMinButton_mouseDownHandler);
                titleBar.removeChild(windowMinButton); // added to title
            }
            // if the restore button is visible
            if(windowRestoreButton != null && (_showMinButton || _showMaxButton))
            {
                if (_showMaxButton) // skin the button depending of the type (maximize or restore)
                {
                    titleBar.removeChild(windowRestoreButton);                                                              
                }
                windowRestoreButton.removeEventListener(MouseEvent.MOUSE_DOWN, windowRestoreButton_mouseDownHandler);
                titleBar.removeEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
            } 
            // if the close button is visible
            if(windowCloseButton != null && _showCloseButton)
            {
                windowCloseButton.removeEventListener(MouseEvent.MOUSE_DOWN, windowCloseButton_mouseDownHandler);
                titleBar.removeChild(windowCloseButton);
            }
        }

        protected function startMaximize():void
        {
            isMaximizing = true;
        }

        protected function endMaximize() : void
        {
            isMaximizing = false;
        }

        protected function startRestore():void
        {
            isMaximizing = true;
        }
		
		protected function startClose():void
		{
			// for override
		}
        
        protected function endRestore() : void
        {
            isMaximizing = false;
        }

		protected function startMinimize():void
		{
		    isMinimizing = true;
		}
		
        protected function endMinimize() : void
        {
            isMinimizing = false;
        }
		
		protected function endClose():void
		{
			// for override
		}
        
		//--------------------------------------
		//  Style properties values
		//--------------------------------------

        public function getHeightBorderThicknessDefault ( ) : Number
        {
            var borderBottom : Number = (parent as Container).getStyle("borderThicknessBottom");
            var borderTop : Number = (parent as Container).getStyle("borderThicknessTop");
            var titleBarHeight : Number = (parent as Container).getStyle("headerHeight");
            
            var totalHeight : Number = 0;
            
            if ( ! isNaN( borderBottom ) )
            {
                totalHeight = borderBottom;
            }
            
            if ( ! isNaN( borderTop ) )
            {
                totalHeight = totalHeight + borderTop;
            }
            
            if ( ! isNaN( titleBarHeight ) )
            {
                totalHeight = totalHeight + titleBarHeight;
            }
            
            return ( totalHeight );
        }
        
        public function getWidthBorderThicknessDefault ( ) : Number
        {
            var borderLeft : Number = (parent as Container).getStyle("borderThicknessLeft");
            var borderRight : Number = (parent as Container).getStyle("borderThicknessRight");
            
            var totalWidth : Number = 0;
            
            if ( ! isNaN( borderLeft ) )
            {
                totalWidth = borderLeft;
            }
            
            if ( ! isNaN( borderRight ) )
            {
                totalWidth = totalWidth + borderRight;
            }
            
            return ( totalWidth );
        }
        

        public function getCloseWindowButtonSkin():Class
        {
            var skin:Class = getStyle("closeWindowButtonSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getCloseWindowButtonDownSkin():Class
        {
            var skin:Class = getStyle("closeWindowButtonDownSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getCloseWindowButtonOverSkin():Class
        {
            var skin:Class = getStyle("closeWindowButtonOverSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getMinimizeWindowButtonSkin():Class
        {
            var skin:Class = getStyle("minimizeWindowButtonSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }


        public function getMinimizeWindowButtonDownSkin():Class
        {
            var skin:Class = getStyle("minimizeWindowButtonDownSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getMinimizeWindowButtonOverSkin():Class
        {
            var skin:Class = getStyle("minimizeWindowButtonOverSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }


        public function getMaximizeWindowButtonSkin():Class
        {
            var skin:Class = getStyle("maximizeWindowButtonSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getMaximizeWindowButtonDownSkin():Class
        {
            var skin:Class = getStyle("maximizeWindowButtonDownSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getMaximizeWindowButtonOverSkin():Class
        {
            var skin:Class = getStyle("maximizeWindowButtonOverSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getRestoreWindowButtonSkin():Class
        {
            var skin:Class = getStyle("restoreWindowButtonSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getRestoreWindowButtonDownSkin():Class
        {
            var skin:Class = getStyle("restoreWindowButtonDownSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }

        public function getRestoreWindowButtonOverSkin():Class
        {
            var skin:Class = getStyle("restoreWindowButtonOverSkin");
            if ( skin )
            {
                return ( skin );
            }
            return ( null ); // Set a default value in here. Could be a ProgramaticSkin class
        }
        
        //--------------------------------------
        //  Skin buttons
        //--------------------------------------

        protected function skinMaximizeButton ( ) : void
        {
            if ( windowRestoreButton )
            {
                windowRestoreButton.setStyle("upSkin", getMaximizeWindowButtonSkin());
                windowRestoreButton.setStyle("overSkin", getMaximizeWindowButtonOverSkin());
                windowRestoreButton.setStyle("downSkin", getMaximizeWindowButtonDownSkin());     
            }
        }

        protected function skinRestoreButton ( ) : void
        {
            if ( windowRestoreButton )
            {
                windowRestoreButton.setStyle("upSkin",getRestoreWindowButtonSkin());
                windowRestoreButton.setStyle("overSkin",getRestoreWindowButtonOverSkin());
                windowRestoreButton.setStyle("downSkin",getRestoreWindowButtonDownSkin());  
            }
        }

        protected function skinCloseButton ( ) : void
        {
            if ( windowCloseButton )
            {
                windowCloseButton.setStyle("upSkin",getCloseWindowButtonSkin());
                windowCloseButton.setStyle("overSkin",getCloseWindowButtonOverSkin());
                windowCloseButton.setStyle("downSkin",getCloseWindowButtonDownSkin());
            }
        }

        protected function skinMinimizeButton ( ) : void
        {
            if ( windowMinButton )
            {
                windowMinButton.setStyle("upSkin", getMinimizeWindowButtonSkin()); // skin the button
                windowMinButton.setStyle("overSkin", getMinimizeWindowButtonOverSkin());
                windowMinButton.setStyle("downSkin", getMinimizeWindowButtonDownSkin());
            }
        }

        //--------------------------------------
        //  Buttons handlers
        //--------------------------------------

		/**
		 * Minimize panel event handler.
		 */
		protected function windowMinButton_mouseDownHandler(event:MouseEvent):void
		{
//			event.stopImmediatePropagation();
			event.preventDefault();
			event.stopPropagation();
		    minimize();
		}

		protected function windowCloseButton_mouseDownHandler ( event : MouseEvent ) : void
		{
			event.preventDefault();
			event.stopPropagation();
		    close();
		}

		/**
		 * Restore panel event handler. If the panel is minimized or maximized the window will be restored
		 * to its original position and size and its listeners will be restored.
		 * If the panel is in its normal size it will be maximized.
		 */
		protected function windowRestoreButton_mouseDownHandler(event:MouseEvent):void
		{
			event.preventDefault();
			// if the panel it's maximized
			if ( (_isMaximized&&_isMinimized) || (!_isMaximized&&!_isMinimized) )
			{
				maximize();
			}
			// if the panel it's going to be maximized
			else
			{    
			    restore();
			}
			invalidateDisplayList();
		}

		protected function panelClickHandler ( event:MouseEvent ):void
		{
            if ( isMinimized )
            {
                windowRestoreButton_mouseDownHandler(event);
            }
		}

		protected function doubleClickHandler ( event:MouseEvent ):void
		{
		    windowRestoreButton_mouseDownHandler(event);
		}

		//--------------------------------------
		//  Effect handlers
		//--------------------------------------
        protected function minimizeEffect_effectEndHandler (event:EffectEvent):void
        {
        	var effect:Effect = Effect(event.currentTarget);
        	effect.removeEventListener(EffectEvent.EFFECT_END,minimizeEffect_effectEndHandler);
        	endMinimizeActions();
        }

        protected function maximizeEffect_effectEndHandler (event:EffectEvent):void
        {
              var effect:Effect = Effect(event.currentTarget);
            effect.removeEventListener(EffectEvent.EFFECT_END,maximizeEffect_effectEndHandler);
            endMaximizeActions();
        }

        protected function restoreEffect_effectEndHandler (event:EffectEvent):void
        {
            var effect:Effect = Effect(event.currentTarget);
            endRestoreActions();
        }

        protected function closeEffect_effectEndHandler (event:EffectEvent):void
        {
            var effect:Effect = Effect(event.currentTarget);
            endCloseActions();
        }

        //--------------------------------------
        //  Events dispatchers
        //--------------------------------------

		/**
		 * Dispatches the MAX_WINDOW event
		 */		
		protected function dispatchMaxEvent (oldWidth:Number = undefined,
                                             oldHeight:Number = undefined, 
											 newWidth:Number = undefined,
											 newHeight:Number = undefined,
                                             oldX:Number = undefined,
                                             oldY:Number = undefined,
											 newX:Number = undefined,
											 newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
										AdvancedPanelEvent.MAX_WINDOW,
										oldWidth,oldHeight,
										newWidth,newHeight,
										oldX,oldY,newX,newY,false,true);
			dispatchEvent(event);
			return(event);
		}
		
		/**
		 * Dispatches the MAXIMIZED_WINDOW event
		 */		
		protected function dispatchMaximizedEvent (oldWidth:Number = undefined,
												   oldHeight:Number = undefined, 
												   newWidth:Number = undefined,
												   newHeight:Number = undefined,
												   oldX:Number = undefined,
												   oldY:Number = undefined,
												   newX:Number = undefined,
												   newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
												AdvancedPanelEvent.MAXIMIZED_WINDOW,
												oldWidth,oldHeight,
												newWidth,newHeight,
												oldX,oldY,newX,newY);
			dispatchEvent(event);
			return(event);
		}
		
		/**
		 * Dispatches the RESTORE_WINDOW event
		 */		
		protected function dispatchRestoreEvent (oldWidth:Number = undefined,
												 oldHeight:Number = undefined, 
												 newWidth:Number = undefined,
												 newHeight:Number = undefined,
												 oldX:Number = undefined,
												 oldY:Number = undefined,
												 newX:Number = undefined,
												 newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
											AdvancedPanelEvent.RESTORE_WINDOW,
											oldWidth,oldHeight,
											newWidth,newHeight,
											oldX,oldY,
											newX,newY,false,true);
			dispatchEvent(event);
			return(event);
		}

		/**
		 * Dispatches the MAXIMIZED_WINDOW event
		 */		
		protected function dispatchRestoredEvent (oldWidth:Number = undefined,
												  oldHeight:Number = undefined, 
												  newWidth:Number = undefined,
												  newHeight:Number = undefined,
												  oldX:Number = undefined,
												  oldY:Number = undefined,
												  newX:Number = undefined,
												  newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
												AdvancedPanelEvent.RESTORED_WINDOW,
												oldWidth,oldHeight,
												newWidth,newHeight,
												oldX,oldY,
												newX,newY);
			dispatchEvent(event);
			return(event);
		}

		/**
		 * Dispatches the MIN_WINDOW event
		 */
		protected function dispatchMinEvent (oldWidth:Number = undefined,
											 oldHeight:Number = undefined, 
											 newWidth:Number = undefined,
											 newHeight:Number = undefined,
											 oldX:Number = undefined,
											 oldY:Number = undefined,
											 newX:Number = undefined,
											 newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
					AdvancedPanelEvent.MIN_WINDOW,oldWidth,oldHeight,
					newWidth,newHeight,oldX,oldY,newX,newY,false,true);
			dispatchEvent(event);
			return(event);
		}

		/**
		 * Dispatches the MINIMIZED_WINDOW event
		 */		
		protected function dispatchMinimizedEvent (oldWidth:Number = undefined,
												   oldHeight:Number = undefined, 
												   newWidth:Number = undefined,
												   newHeight:Number = undefined,
												   oldX:Number = undefined,
												   oldY:Number = undefined,
												   newX:Number = undefined,
												   newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
											AdvancedPanelEvent.MINIMIZED_WINDOW,
											oldWidth,oldHeight,
											newWidth,newHeight,
											oldX,oldY,
											newX,newY);
			dispatchEvent(event);
			return(event);
		}
		
		/**
		 * Dispatches the CLOSE_WINDOW event
		 */
		protected function dispatchCloseEvent (oldWidth:Number = undefined,
											   oldHeight:Number = undefined, 
											   newWidth:Number = undefined,
											   newHeight:Number = undefined,
											   oldX:Number = undefined,
											   oldY:Number = undefined,
											   newX:Number = undefined,
											   newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
					AdvancedPanelEvent.CLOSE_WINDOW,oldWidth,oldHeight,
					newWidth,newHeight,oldX,oldY,newX,newY,false,true); 
			dispatchEvent(event);
			return(event);
		}
		
		/**
		 * Dispatches the CLOSED_WINDOW event
		 */
		protected function dispatchClosedEvent ( panel : DisplayObject = null,
												 oldWidth:Number = undefined,
												 oldHeight:Number = undefined, 
												 newWidth:Number = undefined,
												 newHeight:Number = undefined,
												 oldX:Number = undefined,
												 oldY:Number = undefined,
												 newX:Number = undefined,
												 newY:Number = undefined):AdvancedPanelEvent
		{
			var event:AdvancedPanelEvent = new AdvancedPanelEvent(
											AdvancedPanelEvent.CLOSED_WINDOW,
											oldWidth,oldHeight,newWidth,newHeight,
											oldX,oldY,newX,newY);
			if ( panel )
			{
				panel.dispatchEvent(event);
			}
			else
			{
				dispatchEvent(event);
			}
			return (event);
		}
	}
}